package com.example.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.dto.PageDto;
import com.example.common.vo.PageVo;
import com.example.common.vo.Result;
import com.example.entity.dto.StudentDto;
import com.example.entity.vo.StudentVo;
import com.example.service.pojo.Student;
import com.example.service.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/student")
@Tag(name = "学生管理")
@RequiredArgsConstructor
public class StudentController {

    private final StudentService studentService;

    @GetMapping("{studentId}")
    @Operation(summary = "根据id查询学生信息")
    public Result<StudentVo> getStudentById(@PathVariable String studentId){
        Student student = studentService.getById(studentId);
        StudentVo studentVo = new StudentVo();
        BeanUtils.copyProperties(student, studentVo);
        return Result.success(studentVo);
    }

    @PostMapping
    @Operation(summary = "添加学生信息")
    public Result<Boolean> addStudent(@RequestBody StudentDto studentDto){
        Student student = new Student();
        BeanUtils.copyProperties(studentDto, student);
        boolean save = studentService.save(student);
        return Result.success(save);
    }

    @DeleteMapping("{studentId}")
    @Operation(summary = "根据id删除学生信息")
    public Result<Boolean> deleteStudentById(@PathVariable String studentId){
        boolean remove = studentService.removeById(studentId);
        return Result.success(remove);
    }

    @PutMapping
    @Operation(summary = "修改学生信息")
    public Result<Boolean> updateStudent(@RequestBody StudentDto studentDto){
        Student student = new Student();
        BeanUtils.copyProperties(studentDto, student);
        boolean update = studentService.updateById(student);
        return Result.success(update);
    }

    @PostMapping("page")
    @Operation(summary = "分页查询学生信息")
    public Result<PageVo<StudentVo>> getStudentPage(@RequestBody PageDto<StudentDto> pageDto){
        if(pageDto==null){
            return Result.clientFail("分页参数不能为空", null);
        }
        Page<Student> page = Page.of(pageDto.getPageNum(), pageDto.getPageSize());
        QueryWrapper<Student> wrapper = new QueryWrapper();
        wrapper.like("name", pageDto.getQuery().getName());
        page = studentService.page(page, wrapper);
        List<Student> records = page.getRecords();
        List<StudentVo> collect = records.stream().map(student -> {
            StudentVo studentVo = new StudentVo();
            BeanUtils.copyProperties(student, studentVo);
            return studentVo;
        }).collect(Collectors.toList());
        PageVo<StudentVo> pageVo = new PageVo<>(page.getTotal(), collect);
        return Result.success(pageVo);
    }

}
